<?php

namespace App\Http\Controllers\Auth;

use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Services\Helper;
use DB;

class XdRegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    protected $redirectTo = '/index.html';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        $code = session("register");
        $messages = [
            'code.required' => '验证码必须填写',
            'code.in' => '验证码不正确',
            'phone.required' => '手机必须填写',
            'phone.phone' => '手机格式不正确',
            'phone.unique' => '手机已经被注册',
            'password.required' => '密码必须填写',
            'password.min' => '密码必须大于6位',
        ];

        return Validator::make($data, [
            'code' => 'required|in:'. $code,
            'phone' => 'required|phone|max:255|unique:users',
            'password' => 'required|min:6',
        ],$messages);
    }

    public function register(Request $request)
    {	
        if($request->isMethod('post')) {
            $data['phone'] = $request->input('username');
            $data['username'] = $request->input('username');
            $data['password'] = $request->input('password');
            $data['code'] = $request->input('code');
            $data['uid'] = Helper::createUuid();

            $validatorMsg = $this->validator($data);
            if ($validatorMsg->fails()) {
                return Helper::jsonError($validatorMsg->errors()->first());
            }
            $result = $this->create($data);
            if ($result) {                
                //新建用户       
                $uid = $data['uid'];  
                session(['uid' => $uid]);                        
                return Helper::jsonSuccess('注册成功');                                    
            } else {
                return Helper::jsonError('注册失败，请重试');
            }
        }
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        return User::create([
            'uid' => $data['uid'],
            'username' => $data['username'],
            'phone' => $data['phone'],
            'password' => bcrypt($data['password']),
        ]);
    }
}
